const mqtt = require("mqtt");
const url1 = "mqtt://192.168.18.226"
const url2 = "mqtt://192.168.18.226:1883"
const url3 = "ws://192.168.18.226:19001"

// const client = mqtt.connect("mqtt://test.mosquitto.org");
const client = mqtt.connect(url3);
const jsonMsg = {
  id: '2550ee2f',
  showChild: 1
}

client.on("connect", () => {
  client.subscribe("presence", (err) => {
    if (!err) {
      // client.publish("presence", "Hello mqtt");
      client.publish("presence", JSON.stringify(jsonMsg));
    }
  });
});

client.on("message", (topic, message) => {
  // message is Buffer
  // console.log(message.toString());
  console.log(JSON.parse(message.toString()));
  client.end();
});